Home:ALL Converter>Script output is different between C# Console and PS or CMD

Script output is different between C# Console and PS or CMD

Ask Time:2020-11-11T01:38:07         Author:Lee Jackson

Json Formatter

I am trying to get a list of the installed applications, i have a couple of scripts which do the trick in powershell or from the command window.

but, when i try to run a powerscript command or run a process, i get different data.

the CMD and PowerScript approach gets me 360 applications, but if i run from c# i get 215, and it outputs everything twice ??

simple power script method

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion

the call to run a process

        process = new Process();
        process.StartInfo.FileName = "powershell.exe";
        process.StartInfo.Arguments = @"-executionpolicy unrestricted -File D:\PS-Scripts\InstalledPrograms.ps1";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.Verb = "runas";
        process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
        process.Start();
        process.BeginOutputReadLine();

am I missing something, why does the process call return different data, and also outputs everything twice.

Thanks

Author:Lee Jackson,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64773923/script-output-is-different-between-c-sharp-console-and-ps-or-cmd
yy